package util.converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import javax.faces.convert.Converter;
import util.enumeration.JobPosition;

@FacesConverter(value = "jobPositionConverter", forClass = util.enumeration.JobPosition.class)
public class JobPositionConverter implements Converter {
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {    

        if (value == null || value.length() == 0) {
            return null;
        }

        if(value.equals("MANAGER")) return JobPosition.MANAGER;
        else return JobPosition.NORMAL_EMPLOYEE;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null) {
            return null;
        }

        if (value instanceof JobPosition) {
            JobPosition jobPosition = (JobPosition) value;

            if(jobPosition.equals(jobPosition.MANAGER)) return "MANAGER";
            else return "NORMAL_EMPLOYEE";
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.JobPosition");
        }
    }
}
